package com.aliengen.thingple.thingple;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.aliengen.thingple.beep.DevBeep;
import com.aliengen.thingple.thingple.handler.CMCHandler;
import com.aliengen.thingple.thingple.preferences.PreferencesUtil;
import com.aliengen.thingple.thingple.service.HandsetApplication;

import java.util.Locale;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public abstract class AbstractThingpleActivity extends AppCompatActivity {
    protected HandsetApplication mApplication;

    static CMCHandler handler = new CMCHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        Context ctx = getApplicationContext();
        PreferencesUtil.init(ctx);

        Configuration config = new Configuration();

        String language = PreferencesUtil.get(PreferencesUtil.APP_LANGUAGE, "en");

        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

        DevBeep.init(this);
        this.mApplication = (HandsetApplication) getApplication();
        mApplication.setHandler(handler);
    }

    public void setOperatorName()
    {
        View v = this.findViewById(R.id.operatorTitle);
        if(v != null) {
            Resources res = getResources();
            if(mApplication != null && mApplication.mService != null && mApplication.mService.api != null) {
                String message = String.format(res.getString(R.string.operator_name), mApplication.mService.api.getOperatorName());

                TextView operatorTag = (TextView) v;
                operatorTag.setText(message);
                operatorTag.invalidate();
            }
        }
    }

    public void setText(int viewId, int formatId, String text)
    {
        View v = this.findViewById(viewId);
        if(v != null) {
            Resources res = getResources();
            String message = String.format(res.getString(formatId), text);

            TextView tView = (TextView) v;
            tView.setText(message);
            tView.invalidate();
        }
    }

    public void showToast(final String message) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AbstractThingpleActivity.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void showToast(final int message) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AbstractThingpleActivity.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
